{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e75d4176",
   "metadata": {},
   "source": [
    "# 模块sys：系统相关"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c29ebe5",
   "metadata": {},
   "source": [
    "sys模块是与系统相关的标准库模块："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e355ebd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85d6f652",
   "metadata": {},
   "source": [
    "## 命令行参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3524c9f7",
   "metadata": {},
   "source": [
    "sys模块一个重要的作用是解析命令行的参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "baf1d15c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting print_args.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile print_args.py\n",
    "import sys\n",
    "print(sys.argv)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40b61ddd",
   "metadata": {},
   "source": [
    "运行这个程序："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3e4bee55",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['print_args.py', '1', 'foo']\r\n"
     ]
    }
   ],
   "source": [
    "!python print_args.py 1 foo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "163848c4",
   "metadata": {},
   "source": [
    "`sys.argv`是一个列表，是`python`命令后的各个参数列表，且所有的值都为字符串。如果参数字符串中有空格，需要用引号："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "830b3f2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['print_args.py', '1', 'zhang', 'san', 'zhang san']\r\n"
     ]
    }
   ],
   "source": [
    "!python print_args.py 1 zhang san \"zhang san\" "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cc81b8e",
   "metadata": {},
   "source": [
    "## 系统参数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "464bf26e",
   "metadata": {},
   "source": [
    "查看Python搜索模块的系统路径，不同操作系统不同："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e012517a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['/Users/lijin/Documents/Projects/notes-python3/ch05-Python标准库',\n",
       " '/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',\n",
       " '/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9',\n",
       " '/usr/local/Cellar/python@3.9/3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',\n",
       " '',\n",
       " '/usr/local/lib/python3.9/site-packages']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.path"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24df60c4",
   "metadata": {},
   "source": [
    "## 操作系统信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fd32431",
   "metadata": {},
   "source": [
    "变量`sys.platform`用来显示当前操作系统的相关信息："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "631fc798",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'darwin'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sys.platform"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5004bee",
   "metadata": {},
   "source": [
    "不同的操作系统对应不同的值：\n",
    "- Windows: win32。\n",
    "- Mac: darwin。\n",
    "- Linux: linux2。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2297d69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
